package es.uc3m.setichat.utils;

import java.security.NoSuchAlgorithmException;
import java.util.Random;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class GenerateKeyIV {
	
	private	byte [] key;
	private	byte [] IV;
	
	public byte[] getKey() {
		return key;
	}

	public void setKey(byte[] key) {
		this.key = key;
	}

	public byte[] getIV() {
		return IV;
	}

	public void setIV(byte[] iV) {
		IV = iV;
	}

	public void generateKey(){
		KeyGenerator kgen = null;
		try {
			kgen = KeyGenerator.getInstance("AES");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		kgen.init(128); 
		SecretKey skey = kgen.generateKey();
		setKey(skey.getEncoded());
	}
	
	public void generateIV(){
		Random rand = new Random(System.currentTimeMillis());
		int aux;
		byte result[] = new byte[16];
		for(int i=0;i<16;i++){
			aux = rand.nextInt(256);
			result[i] = (byte) aux;
		}
		setIV(result);
	}
}
